package com.ruoyi.performance.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.performance.domain.PerfTemplateMitem;
import com.ruoyi.performance.dto.PerfTemplateMitemDto;
import com.ruoyi.performance.service.PerfTemplateMitemService;
import com.ruoyi.performance.utils.MyUtils;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YL
 * @date 2020-07-22
 * @description 绩效模板主项控制层
 */
@RestController
@RequestMapping("templatemitem")
public class PerfTemplateMitemController extends MyUtils {

    @Resource
    private PerfTemplateMitemService perfTemplateMitemService;

    /**
     * 根据id查找绩效模板主项
     *
     * @param id 绩效模板主项id
     * @return 绩效模板主项明细
     */
    @GetMapping("get/{id}")
    public R list(@PathVariable("id") int id) {
        Map<String, Object> m = new HashMap<>(16);
        m.put("rows", perfTemplateMitemService.selectPerfTemplateMitemById(id));
        return R.ok(m);
    }

    /**
     * 查询绩效模板主项明细列表
     */
    @GetMapping("list")
    public R list(PerfTemplateMitemDto perfTemplateMitemDto) {
        startPage();
        return result(perfTemplateMitemService.selectPerfTemplateMitemList(perfTemplateMitemDto));
    }

    /**
     * 新增保存绩效模板主项明细
     */
    @PostMapping("save")
    public Map<String,Object> addSave(@RequestBody PerfTemplateMitemDto perfTemplateMitemDto) {
        
        Map<String,Object> r = new HashMap<String,Object>();
        Map<String,Integer> id = new HashMap<String,Integer>();
    	int result = perfTemplateMitemService.insertPerfTemplateMitem(perfTemplateMitemDto);
    	if (result > 0) 
    	{   
    		id.put("id", result);
        	r.put("code", 0);
    		r.put("msg", "success");
    		r.put("rows", id);
    		return r;
    	}
    	else
    	{
        	r.put("code", 500);
    		r.put("msg", "新增主项明细失败，请联系管理员！");
    		return r;
    	}  


    }

    /**
     * 修改保存绩效模板主项明细
     */
    @PostMapping("update")
    public Map<String,String> editSave(@RequestBody Map<String, List<PerfTemplateMitem>> datas) {
        return jacResultMsg(perfTemplateMitemService.updatePerfTemplateMitem(datas));
    }

    /**
     * 删除绩效模板主项
     */
    @PostMapping("remove")
    public Map<String,String> remove(String ids) {
        return jacResultMsg(perfTemplateMitemService.deletePerfTemplateMitemById(ids));
    }

}
